package com.jy.wms.service;

import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsDistributionMapper;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsBoxMapper;
import io.swagger.models.auth.In;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
    * @ClassName: WmsBoxService.java
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @Author Jane
    * @Date Wed Oct 25 10:02:09 CST 2017
    */

    @Service
    public class WmsBoxService extends BaseService<WmsBoxMapper,WmsBox> {


        /**
         *装箱_Jane
         * @param ids 发运订单主键ID列表
         * @return int
         */
        @BaseServiceDescribe(moduleName = "装箱",btnName = "确认装箱")
        @Transactional
        public void casingContainerazation(List<String> ids,String ckid,String sshzid)throws Exception{
                for(String id:ids) {
                    //根据ID查询So订单各种状态是否满足
                    WmsShipmentOrder so = dao.baseSelectBySokey(id);
                    /**(1)	SO状态为“部分分配”/“全部分配”
                     (2)	装箱状态为“未装箱”/“部分装箱”
                     (3)	拆单状态为“已拆单”
                     (4)	货主管理中“是否切箱”选项为是。
                     */
                    if (("1".equals(so.getFyzt()) || "2".equals(so.getFyzt())) && ("0".equals(so.getZxzt()) || "1".equals(so.getZxzt())) && "1".equals(so.getCdzt()) && "1".equals(so.getZxqx())) {
                        //	(6)根据发运订单找寻分配明细“是否装箱”状态为“未装箱”的分配明细信息。
                        List<WmsDistribution> distributions = dao.seachDistribution(id);
                        int xxh = 0;
                        List<WmsDistribution> sx = new ArrayList<WmsDistribution>();
                        for (WmsDistribution dis : distributions) {
                            xxh++;
                            //	(7)根据分配明细中的拆单ID,比对分配明细的数量是否与拆单明细数量一致
                            WmsSplit split = dao.seackSplit(dis.getSscdid());
                            //(8)对比拆单和分配数量
                            if (Integer.parseInt(dis.getFpsl()) == Integer.parseInt(split.getSl())) {
                                //(8--1)拆单数量与未装箱分配数量相等------整箱标记为整箱 散箱标记为散箱
                                if ("0".equals(split.getZslx())) {
                                    //整箱标记为整箱
                                    WmsBox box = new WmsBox();
                                    box.setId(UUIDUtils.uuid());
                                    box.setSsfpid(dis.getId());
                                    box.setSsfydid(split.getSsfyddid());
                                    box.setSsfymxid(dis.getSsfymxid());
                                    box.setHpid(dis.getHpid());
                                    box.setSl(dis.getFpsl());
                                    box.setXbh(GeneratorBoxNumber());
                                    box.setXxh(xxh);
                                    box.setZzxid("0");
                                    box.setZslx("0");
                                    box.setCkid(ckid);
                                    dao.baseInsert(box);
                                    WmsDistribution d = new WmsDistribution();
                                    d.setId(dis.getId());
                                    d.setZxzt("1");
                                    dao.updateDistribution(d);
                                } else if ("1".equals(split.getZslx())) {
                                    //散箱标记需要拼箱
                                    sx.add(dis);//散箱标记加入另外一个集合 等待拼箱
                                } else {
                                    throw new ServiceException("拆单整散标记有误");
                                }
                            } else if (Integer.parseInt(dis.getFpsl()) < Integer.parseInt(split.getSl())) {
                                //(8--2)	拆单数量大于未装箱分配数量------散箱
                                sx.add(dis);//都是散箱加入另外集合 等待拼箱
                            } else {
                                //(8--3)分配数量大于拆单数量理论不会出现这种情况 异常
                                throw new ServiceException("分配或拆单数据异常");
                            }
                        }
                        //整合散箱准备拼箱
                        Integer maxXxh = dao.seachMaxXxhBySsfydid(id);//获取最大箱序号
                        String hpType = "";//记录货品类型
                        if (sx.size() > 0) {
                            for (WmsDistribution d : sx) {
                                WmsGoodsType sfhf = dao.selectGoodsSfhf(d.getHpid());
                                if ("0".equals(sfhf.getSfhf())) {
                                    //不可以混放
                                    if (hpType.equals(d.getZlid())) {
                                        //即为一类货品可放到一起
                                        WmsBox box = new WmsBox();
                                        box.setId(UUIDUtils.uuid());
                                        box.setCkid(ckid);
                                        box.setSsfpid(d.getId());
                                        box.setSsfydid(id);
                                        box.setSsfymxid(d.getSsfymxid());
                                        box.setHpid(d.getHpid());
                                        box.setSl(d.getFpsl());
                                        box.setXbh("");
                                        box.setXxh(maxXxh);
                                        box.setZzxid("0");
                                        box.setZslx("1");
                                        dao.baseInsert(box);
                                    } else {
                                        //不是一类货品，单独放置
                                        maxXxh += maxXxh;
                                        WmsBox box = new WmsBox();
                                        box.setId(UUIDUtils.uuid());
                                        box.setCkid(ckid);
                                        box.setSsfpid(d.getId());
                                        box.setSsfydid(id);
                                        box.setSsfymxid(d.getSsfymxid());
                                        box.setHpid(d.getHpid());
                                        box.setSl(d.getFpsl());
                                        box.setXbh("");
                                        box.setXxh(maxXxh);
                                        box.setZzxid("0");
                                        box.setZslx("1");
                                        dao.baseInsert(box);

                                    }
                                    hpType = d.getZlid();//记录货品类型
                                } else if ("1".equals(sfhf.getSfhf())) {
                                    //可以混放
                                    WmsBox box = new WmsBox();
                                    box.setId(UUIDUtils.uuid());
                                    box.setCkid(ckid);
                                    box.setSsfpid(d.getId());
                                    box.setSsfydid(id);
                                    box.setSsfymxid(d.getSsfymxid());
                                    box.setHpid(d.getHpid());
                                    box.setSl(d.getFpsl());
                                    box.setXbh("");
                                    box.setXxh(maxXxh + 1);
                                    box.setZzxid("0");
                                    box.setZslx("1");
                                    dao.baseInsert(box);
                                } else {
                                    throw new ServiceException("货品混放标记异常");
                                }

                            }
                            //装箱
                            //获取要装箱的总体积和总重量
                            List<WmsBox> ztj = dao.seachZtjAndZzl(id);
                            for (WmsBox box : ztj) {
                                //获取周转箱的信息
                                List<WmsTurnoverBox> turnoverBoxes = dao.seachTurnoverMx(sshzid,ckid);
                                Double tj = Double.parseDouble(box.getZtj());//记录货品体积
                                Double zl = Double.parseDouble(box.getZzl());//记录货品重量
                                //选箱子
                                String jl = "";//记录箱
                                List<String> xid = new ArrayList<>();
                                for (int i = 0; i < turnoverBoxes.size(); i++) {
                                    if (xid.size() == 1) {
                                        //获取要装箱的货品
                                        List<WmsBox> NoBhBox = dao.seachNoXbhBySsfydid(id, box.getXxh());
                                        WmsTurnoverBox tBox = dao.seachTurnoverXX(xid.get(0));
                                        for (WmsBox ckg : NoBhBox) {
                                            if (Double.parseDouble(ckg.getGoodsC()) > Double.parseDouble(tBox.getC()) || Double.parseDouble(ckg.getGoodsK()) > Double.parseDouble(tBox.getK()) || Double.parseDouble(ckg.getGoodsG()) > Double.parseDouble(tBox.getG())) {
                                                if (i != 0) {
                                                    xid.set(0, turnoverBoxes.get(i - 1).getId());
                                                }
                                                tj = Double.parseDouble(box.getZtj()) - Double.parseDouble(turnoverBoxes.get(i - 1).getKytj());
                                                zl = Double.parseDouble(box.getZzl()) - Double.parseDouble(turnoverBoxes.get(i - 1).getZdtj());
                                            }
                                        }
                                        //货品总体积大于箱体积 则选择一个大箱子
                                        if (tj >= Double.parseDouble(turnoverBoxes.get(i - 1).getKytj()) && zl >= Double.parseDouble(turnoverBoxes.get(i - 1).getZdtj())) {
                                            if ("".equals(jl)) {

                                                xid.add(turnoverBoxes.get(i - 1).getId());
                                                tj -= Double.parseDouble(turnoverBoxes.get(i - 1).getKytj());
                                                zl -= Double.parseDouble(turnoverBoxes.get(i - 1).getZdtj());
                                            } else {
                                                xid.add(jl);
                                                break;
                                            }
                                        } else if (tj <= Double.parseDouble(turnoverBoxes.get(i - 1).getKytj()) && zl >= Double.parseDouble(turnoverBoxes.get(i - 1).getZdtj())) {
                                            if ("".equals(jl)) {
                                                xid.add(turnoverBoxes.get(i - 1).getId());
                                                tj -= Double.parseDouble(turnoverBoxes.get(i - 1).getKytj());
                                                zl -= Double.parseDouble(turnoverBoxes.get(i - 1).getZdtj());
                                            } else {
                                                xid.add(jl);
                                                break;
                                            }
                                        } else {
                                            //货品体积小于箱体积 则记录箱体积下次循环有则换小一号箱子
                                            jl = turnoverBoxes.get(i).getId();
                                        }
                                    } else {
                                        //货品总体积大于箱体积 则选择一个大箱子
                                        if (tj >= Double.parseDouble(turnoverBoxes.get(i).getKytj()) && zl >= Double.parseDouble(turnoverBoxes.get(i).getZdtj())) {
                                            if ("".equals(jl)) {

                                                xid.add(turnoverBoxes.get(i).getId());
                                                tj -= Double.parseDouble(turnoverBoxes.get(i).getKytj());
                                                zl -= Double.parseDouble(turnoverBoxes.get(i).getZdtj());
                                            } else {
                                                xid.add(jl);
                                                break;
                                            }
                                        } else if (tj <= Double.parseDouble(turnoverBoxes.get(i).getKytj()) && zl >= Double.parseDouble(turnoverBoxes.get(i).getZdtj())) {
                                            if ("".equals(jl)) {
                                                xid.add(turnoverBoxes.get(i).getId());
                                                tj -= Double.parseDouble(turnoverBoxes.get(i).getKytj());
                                                zl -= Double.parseDouble(turnoverBoxes.get(i).getZdtj());
                                            } else {
                                                xid.add(jl);
                                                break;
                                            }
                                        } else {
                                            //货品体积小于箱体积 则记录箱体积下次循环有则换小一号箱子
                                            jl = turnoverBoxes.get(i).getId();
                                        }
                                    }

                                }
                                //给散箱明细添加箱信息则装箱完成
                                List<WmsBox> NoBhBox = dao.seachNoXbhBySsfydid(id, box.getXxh());
                                for (String zzxid : xid) {
                                    Double qzl = 0d;//记录箱中的重量
                                    Double qtj = 0d;//记录箱中的体积
                                    WmsTurnoverBox zzx = dao.seachTurnoverXX(zzxid);
                                    for (WmsBox ms : NoBhBox) {
                                        //生成一个箱编号
                                        String xbh = GeneratorBoxNumber();
                                        //把当前货品放如箱子中
                                        //判断箱子是否还能装
                                        if (qzl <= Double.parseDouble(zzx.getZdzl()) && qtj <= Double.parseDouble(zzx.getKytj())) {
                                            //记录箱子还能装多少
                                            Double jzl = Double.parseDouble(zzx.getZdzl()) - qzl;
                                            Double jtj = Double.parseDouble(zzx.getKytj()) - qtj;
                                            int sl = (int) (jtj / Double.parseDouble(ms.getGoodsTj()));
                                            if (sl > Integer.parseInt(ms.getSl()) && jzl >= Double.parseDouble(ms.getGoodsZl()) * Double.parseDouble(ms.getSl())) {//一种货品直接都可以装进去
                                                WmsBox b = new WmsBox();
                                                b.setZzxid(zzxid);
                                                b.setXbh(xbh);
                                                b.setId(ms.getId());
                                                dao.baseUpdateByPrimaryKeySelective(b);
                                                WmsDistribution d = new WmsDistribution();
                                                d.setId(ms.getSsfpid());
                                                d.setZxzt("1");
                                                dao.updateDistribution(d);
                                                qtj += Double.parseDouble(ms.getGoodsTj()) * Double.parseDouble(ms.getSl());
                                                qzl += Double.parseDouble(ms.getGoodsZl()) * Double.parseDouble(ms.getSl());
                                            } else if (sl > Integer.parseInt(ms.getSl()) && jzl < Double.parseDouble(ms.getGoodsZl()) * Double.parseDouble(ms.getSl())) {//体积可以 但是重量不行的时候分开
                                                //计算体积还能装多少个
                                                int tsl = (int) (jzl / Double.parseDouble(ms.getGoodsZl()));
                                                WmsBox b = new WmsBox();
                                                b.setZzxid(zzxid);
                                                b.setXbh(xbh);
                                                b.setSl(String.valueOf(tsl));
                                                b.setId(ms.getId());
                                                dao.baseUpdateByPrimaryKeySelective(b);
                                                WmsDistribution d = new WmsDistribution();
                                                d.setId(ms.getSsfpid());
                                                d.setZxzt("1");
                                                dao.updateDistribution(d);
                                                //然后把剩下的新增一条
                                                if((Integer.parseInt(ms.getSl()) - tsl)>0) {
                                                    WmsBox n = new WmsBox();
                                                    n.setId(UUIDUtils.uuid());
                                                    n.setSsfydid(ms.getSsfydid());
                                                    n.setSsfymxid(ms.getSsfymxid());
                                                    n.setXxh(ms.getXxh());
                                                    n.setSl(String.valueOf(Integer.parseInt(ms.getSl()) - tsl));
                                                    n.setHpid(ms.getHpid());
                                                    n.setXbh("");
                                                    n.setZzxid("0");
                                                    n.setZslx("1");
                                                    dao.baseInsert(n);
                                                }
                                            } else if (sl < Integer.parseInt(ms.getSl()) && jzl >= Double.parseDouble(ms.getGoodsZl()) * Double.parseDouble(ms.getSl())) {
                                                WmsBox b = new WmsBox();
                                                b.setZzxid(zzxid);
                                                b.setSl(String.valueOf(sl));
                                                b.setXbh(xbh);
                                                b.setId(ms.getId());
                                                dao.baseUpdateByPrimaryKeySelective(b);
                                                WmsDistribution d = new WmsDistribution();
                                                d.setId(ms.getSsfpid());
                                                d.setZxzt("1");
                                                dao.updateDistribution(d);
                                                //然后把剩下的新增一条
                                                if((Integer.parseInt(ms.getSl()) - sl)>0) {
                                                    WmsBox n = new WmsBox();
                                                    n.setId(UUIDUtils.uuid());
                                                    n.setSsfydid(ms.getSsfydid());
                                                    n.setSsfymxid(ms.getSsfymxid());
                                                    n.setXxh(ms.getXxh());
                                                    n.setSl(String.valueOf(Integer.parseInt(ms.getSl()) - sl));
                                                    n.setHpid(ms.getHpid());
                                                    n.setXbh("");
                                                    n.setZzxid("0");
                                                    n.setZslx("1");
                                                    dao.baseInsert(n);
                                                }
                                            } else {
                                                //计算体积还能装多少个
                                                int tsl = (int) (jzl / Double.parseDouble(ms.getGoodsZl()));
                                                int zsl = 0;
                                                if (tsl < sl) {
                                                    zsl = tsl;
                                                } else {
                                                    zsl = sl;
                                                }
                                                WmsBox b = new WmsBox();
                                                b.setZzxid(zzxid);
                                                b.setSl(String.valueOf(zsl));
                                                b.setXbh(xbh);
                                                b.setId(ms.getId());
                                                dao.baseUpdateByPrimaryKeySelective(b);
                                                WmsDistribution d = new WmsDistribution();
                                                d.setId(ms.getSsfpid());
                                                d.setZxzt("1");
                                                dao.updateDistribution(d);
                                                //然后把剩下的新增一条
                                                if((Integer.parseInt(ms.getSl()) - zsl)>0) {
                                                    WmsBox n = new WmsBox();
                                                    n.setId(UUIDUtils.uuid());
                                                    n.setSsfpid(ms.getSsfpid());
                                                    n.setSsfydid(ms.getSsfydid());
                                                    n.setSsfymxid(ms.getSsfymxid());
                                                    n.setXxh(ms.getXxh());
                                                    n.setSl(String.valueOf(Integer.parseInt(ms.getSl()) - zsl));
                                                    n.setHpid(ms.getHpid());
                                                    n.setXbh("");
                                                    n.setZzxid("0");
                                                    n.setZslx("1");
                                                    dao.baseInsert(n);
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                            //修改箱序号
                            int max = dao.seachMaxXxh(id);
                            List<WmsBox> wmsBoxes = dao.seachSx(id);
                            String xbh = "";
                            for (WmsBox wms : wmsBoxes) {
                                if (xbh.equals(wms.getXbh())) {
                                    WmsBox b = new WmsBox();
                                    b.setId(wms.getId());
                                    b.setXxh(max);
                                    dao.baseUpdateByPrimaryKeySelective(b);
                                } else {
                                    xbh = wms.getXbh();
                                    max = max + 1;
                                    WmsBox b = new WmsBox();
                                    b.setId(wms.getId());
                                    b.setXxh(max);
                                    dao.baseUpdateByPrimaryKeySelective(b);
                                }
                            }
                            //判断最后一箱的体积是或否小于前一箱的浮动容积 小于则 放如前一箱  大于则单独装一箱
                            List<WmsBox> box = dao.seachBoxByMaxXxh(id);//获取最后一箱的信息
                            WmsBox zdtj = dao.seachZhyxTj(id);//获取积最后一箱的体积
                            List<String> dd = dao.selectByzzxid(id);//获取零散周转箱ID
                            if(dd.size()>1){
                            for (String b : dd) {
                                WmsTurnoverBox tb = dao.seachTurnoverXX(b);
                                if (Double.parseDouble(tb.getFdrj()) > Double.parseDouble(zdtj.getZtj())) {
                                    //放入这个箱子 最后一个箱子取消
                                    for (WmsBox x : box) {
                                        WmsBox ox = new WmsBox();
                                        ox.setId(x.getId());
                                        ox.setXxh(x.getXxh());
                                        ox.setZzxid(b);
                                        dao.baseUpdateByPrimaryKeySelective(ox);
                                    }
                                    break;
                                }
                            }
                        }else{
                                int maxXh = dao.seachMaxXxh(id);
                                //只有一个箱子则给最后一个箱子写上箱信息
                                WmsTurnoverBox boxmin=dao.seachZxtjBox(sshzid,ckid);
                                for (WmsBox x : box) {
                                    WmsBox ox = new WmsBox();
                                    ox.setId(x.getId());
                                    ox.setXxh(maxXh+1);
                                    ox.setXbh(GeneratorBoxNumber());
                                    ox.setZzxid(boxmin.getId());
                                    dao.baseUpdateByPrimaryKeySelective(ox);
                                }
                            }
                        }

                        //修改发运单的装箱状态
                        List<WmsShipmentOrderDetailed> orderDetaileds = dao.selectShipmentByMxid(id);
                        int bf = 0;
                        int qb = 0;
                        int qt = 0;
                        for (WmsShipmentOrderDetailed shipmentOrderDetailed : orderDetaileds) {
                            if (Integer.parseInt(shipmentOrderDetailed.getYqsl()) > dao.selecBoxByMxid(shipmentOrderDetailed.getId()) && dao.selecBoxByMxid(shipmentOrderDetailed.getId()) > 0) {
                                bf = 1;
                            } else if (Integer.parseInt(shipmentOrderDetailed.getYqsl()) == dao.selecBoxByMxid(shipmentOrderDetailed.getId())) {
                                qb = 1;
                            } else {
                                qt = 1;
                            }
                        }
                        WmsShipmentOrder order = new WmsShipmentOrder();
                        order.setId(id);
                        Integer max=dao.boxMaxXh(id);
                        if(max>5){
                            order.setCyslb("2");
                        }else{
                            order.setCyslb("1");
                        }
                        if (qb == 1 && bf == 0 && qt == 0) {
                            order.setZxzt("2");
                            dao.updateShipmentOrder(order);
                        } else if (bf == 1) {
                            order.setZxzt("1");
                            dao.updateShipmentOrder(order);
                        }
                    } else {
                        throw new ServiceException("所选订单不符合装箱要求，不可装箱");
                    }
                }
        }
    /**
     *生成箱编号（生成箱编号）
     * @return int
     */
    @BaseServiceDescribe(moduleName = "装箱",btnName = "生成箱编号")
    public String GeneratorBoxNumber()throws  ServiceException{
        int num;
        int fnum;
        String xbh;
        try {
            do {
                Random random=new Random();
                num = random.nextInt(999-100+1)+100;
                SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
                Date time = new Timestamp(System.currentTimeMillis());
                String xbhno = sdf.format(time);
                xbh ="X"+xbhno + num;
                fnum = dao.selectCountXbh(xbh);
            } while (fnum != 0);
            return xbh;
        } catch (Exception e) {
            throw new ServiceException("生成箱编号失败",e);
        }
    }
    /**
         * 分页查询
         * @param entity 实体
         * @return Page<WmsBox>
         */
    @BaseServiceDescribe(moduleName = "装箱",btnName = "分页查询装箱信息")
    public JSONObject selectZxmxByFydid(WmsBox entity ){
        JSONObject json = new JSONObject();
        PageHelper.startPage(entity.getPage(),entity.getRows());
        Page<WmsBox> list = new Page<WmsBox>();
        list=(Page<WmsBox>)dao.selectZxmxByFydid(entity);
        json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        json.put("sobh",list.get(0).getSobh());
        Double xzzl=0d;
        int sxs=0;
        int zxs=0;
        String xbh="";//记录散箱周装箱编号，用于判断是否同一箱记录散箱数量
        for (WmsBox wmsBox : list) {
            if("0".equals(wmsBox.getZzxid())){
                xzzl+=Double.parseDouble(wmsBox.getZzl());
                zxs+=1;
            }else{
                if("".equals(xbh)){
                    xzzl+=Double.parseDouble(wmsBox.getZzl());
                    sxs+=1;
                }else{
                    if(wmsBox.getXbh().equals(xbh)){
                        xzzl+=Double.parseDouble(wmsBox.getZzl());
                    }
                }
                xbh=wmsBox.getXbh();
            }
        }
        json.put("xzzl",xzzl);
        json.put("sxs",sxs);
        json.put("zxs",zxs);

        return json;
    }
    /**
     * 查询装箱打印信息（发运单ID）_Jane
     * @param ssfydid 发运订单ID
     * @return Page<WmsBox>
     */
    @BaseServiceDescribe(moduleName = "装箱",btnName = "打印装箱信息")
    public JSONObject selectDyxxByFydid(String ssfydid,String ckmc,String show_rent,String telephone,String dz){
        JSONObject json = new JSONObject();
        List<WmsBox> list = new ArrayList<>();
        list=dao.selectDyxxByFydid(ssfydid);
        for (WmsBox wmsBox : list) {
            List<WmsBox> boxMx=dao.selectDyOneXxByFydid(ssfydid,wmsBox.getXbh());
            wmsBox.setBoxMx(boxMx);
            wmsBox.setCkmc(ckmc);
            wmsBox.setShow_rent(show_rent);
            wmsBox.setKhdz(dz);
            wmsBox.setCkdh(telephone);
        }
        json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        return json;
    }
    /**
     * 取消装箱_Jane
     * @param ids 所属发运订单ID
     * @return Page<WmsBox>
     */
    @BaseServiceDescribe(moduleName = "装箱",btnName = "取消装箱")
    @Transactional
    public void cancelEncasement(List<String> ids)throws Exception {
        for(String id:ids){
            Integer count=dao.baseDeleteByPrimaryKey(id);
            if(count>0){
                List<WmsShipmentOrderDetailed> orderDetaileds = dao.selectShipmentByMxid(id);
                int bf = 0;
                int qb = 0;
                int qt = 0;
                for (WmsShipmentOrderDetailed shipmentOrderDetailed : orderDetaileds) {
                    if (Integer.parseInt(shipmentOrderDetailed.getYqsl()) > dao.selecBoxByMxid(shipmentOrderDetailed.getId()) && dao.selecBoxByMxid(shipmentOrderDetailed.getId()) > 0) {
                        bf = 1;
                    } else if (Integer.parseInt(shipmentOrderDetailed.getYqsl()) == dao.selecBoxByMxid(shipmentOrderDetailed.getId())) {
                        qb = 1;
                    } else {
                        qt = 1;
                    }
                }
                WmsShipmentOrder order = new WmsShipmentOrder();
                order.setId(id);
                if (qb == 1 && bf == 0 && qt == 0) {
                    order.setZxzt("2");
                    dao.updateShipmentOrder(order);
                } else if (bf == 1) {
                    order.setZxzt("1");
                    dao.updateShipmentOrder(order);
                }else if(qb==0&&bf==0&&qt==1){
                    order.setZxzt("0");
                    dao.updateShipmentOrder(order);
                }
            }else{
                throw new ServiceException("此订单没有装箱明细，取消装箱失败");
            }
        }

    }
}
